/************************************************************
* @file gkt_debug.h
************************************************************/

#ifndef GKT_DEBUG_H
#define GKT_DEBUG_H

#include "g_1903.h"
#include "gkt_types.h"
#include "gkt_stdio.h"

#ifdef __cplusplus
extern "C" {
#endif

#define GKT_LOG_LEVEL_NONE	0
#define GKT_LOG_LEVEL_FATAL	1
#define GKT_LOG_LEVEL_ERROR	2
#define GKT_LOG_LEVEL_WARN	3
#define GKT_LOG_LEVEL_INFO	4
#define GKT_LOG_LEVEL_TRACE	5

#ifdef GKT_CONFIG_LOG_LEVEL
#define GKT_LOG_LEVEL_L2(level_name)	GKT_LOG_LEVEL_##level_name
#define GKT_LOG_LEVEL_L1(level_name)	GKT_LOG_LEVEL_L2(level_name)
#define GKT_LOG_LEVEL	GKT_LOG_LEVEL_L1(GKT_CONFIG_LOG_LEVEL)
#else
#define GKT_LOG_LEVEL	GKT_LOG_LEVEL_ERROR
#endif

//#define gkt_printf(fmt, args...) MyPrintf(fmt, ##args)

#ifdef _MSC_VER

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_FATAL)
#define gkt_fatal(fmt, ...)	\
	gkt_printf("[FATAL] " fmt, __VA_ARGS__)
#else
#define gkt_fatal(fmt, ...)	do {} while(0)
#endif

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_ERROR)
#define gkt_error(fmt, ...)	\
	gkt_printf("[ERROR] " fmt, __VA_ARGS__)
#else
#define gkt_error(fmt, ...)	do {} while(0)
#endif

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_WARN)
#define gkt_warning(fmt, ...)	\
	gkt_printf("[WARN] " fmt, __VA_ARGS__)
#else
#define gkt_warning(fmt, ...)	do {} while(0)
#endif

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_INFO)
#define gkt_info(fmt, ...)	\
	gkt_printf("[INFO] " fmt, __VA_ARGS__)
#else
#define gkt_info(fmt, ...)	do {} while(0)
#endif

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_TRACE)
#define gkt_trace(fmt, ...)	\
	gkt_printf("[TRACE] " fmt, __VA_ARGS__)
#else
#define gkt_trace(fmt, ...)	do {} while(0)
#endif

#else

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_FATAL)
#define gkt_fatal(fmt, args...)	\
	gkt_printf("[FATAL] " fmt, ##args)
#else
#define gkt_fatal(fmt, args...)	do {} while(0)
#endif

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_ERROR)
#define gkt_error(fmt, args...)	\
	gkt_printf("[ERROR] " fmt, ##args)
#else
#define gkt_error(fmt, args...)	do {} while(0)
#endif

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_WARN)
#define gkt_warning(fmt, args...)	\
	gkt_printf("[WARN] " fmt, ##args)
#else
#define gkt_warning(fmt, args...)	do {} while(0)
#endif

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_INFO)
#define gkt_info(fmt, args...)	\
	gkt_printf("[INFO] " fmt, ##args)
#else
#define gkt_info(fmt, args...)	do {} while(0)
#endif

#if (GKT_LOG_LEVEL >= GKT_LOG_LEVEL_TRACE)
#define gkt_trace(fmt, args...)	\
	gkt_printf("[TRACE] " fmt, ##args)
#else
#define gkt_trace(fmt, args...)	do {} while(0)
#endif

#endif


#if defined(GKT_CONFIG_DEBUG_ENABLE) && GKT_CONFIG_DEBUG_ENABLE
#define GKT_ASSERT(_exp_)	\
	do {	\
		if (!(_exp_)) {	\
			gkt_printf("ASSERT: <%s><%d> express \"" #_exp_ "\" check failed.\n", \
				__FUNCTION__, __LINE__);	\
			__BKPT(0);	\
			while(1);	\
		}	\
	} while(0)
#define GKT_RETURN_IF_NOT(_exp_, _retval_)	\
	do {	\
		if (!(_exp_)) {	\
			gkt_printf("ASSERT: <%s><%d> express \"" #_exp_ "\" check failed.\n", \
				__FUNCTION__, __LINE__);	\
			return (_retval_);	\
		}	\
	} while(0)
#else
#define GKT_ASSERT(_exp_)	do {} while(0)
#define GKT_RETURN_IF_NOT(_exp_, _retval_)	\
	do { if (!(_exp_)) { return (_retval_); } } while(0)
#endif

#ifdef __cplusplus
}
#endif

#endif	/* GKT_DEBUG_H */


